home *** CD-ROM | disk | FTP | other *** search
/ Power Programmierung 2 / Power-Programmierung CD 2 (Tewi)(1994).iso / c / library / dos / communic / pcmail / main / msd_dir.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-06-05  |  5.3 KB  |  483 lines

  1. #ifdef    MSDOS
  2.  
  3. /*
  4.  
  5.  * @(#)msd_dir.c 1.4 87/11/06    Public Domain.
  6.  
  7.  *
  8.  
  9.  *  A public domain implementation of BSD directory routines for
  10.  
  11.  *  MS-DOS.  Written by Michael Rendell ({uunet,utai}michael@garfield),
  12.  
  13.  *  August 1897
  14.  
  15.  *
  16.  
  17.  * Return file names in lower case W.Z. Venema (wswietse@lso.win.tue.nl)
  18.  
  19.  * Aug 1988
  20.  
  21.  *
  22.  
  23.  * Added special-case code for the root directory WZV 891218
  24.  
  25.  */
  26.  
  27.  
  28.  
  29. #include    <sys/types.h>
  30.  
  31. #include    <sys/stat.h>
  32.  
  33. #include    "msd_dir.h"    /* was: <sys/dir.h> */
  34.  
  35. #include    <malloc.h>
  36.  
  37. #include    <string.h>
  38.  
  39. #include    <dos.h>
  40.  
  41. #include    <ctype.h>    /* for upper->lower case code */
  42.  
  43.  
  44.  
  45. #ifndef    NULL
  46.  
  47. # define    NULL    0
  48.  
  49. #endif    /* NULL */
  50.  
  51.  
  52.  
  53. #ifndef    MAXPATHLEN
  54.  
  55. # define    MAXPATHLEN    255
  56.  
  57. #endif    /* MAXPATHLEN */
  58.  
  59.  
  60.  
  61. /* attribute stuff */
  62.  
  63. #define    A_RONLY        0x01
  64.  
  65. #define    A_HIDDEN    0x02
  66.  
  67. #define    A_SYSTEM    0x04
  68.  
  69. #define    A_LABEL        0x08
  70.  
  71. #define    A_DIR        0x10
  72.  
  73. #define    A_ARCHIVE    0x20
  74.  
  75.  
  76.  
  77. /* dos call values */
  78.  
  79. #define    DOSI_FINDF    0x4e
  80.  
  81. #define    DOSI_FINDN    0x4f
  82.  
  83. #define    DOSI_SDTA    0x1a
  84.  
  85.  
  86.  
  87. #define    Newisnull(a, t)        ((a = (t *) malloc(sizeof(t))) == (t *) NULL)
  88.  
  89. #define    ATTRIBUTES        (A_DIR | A_HIDDEN | A_SYSTEM)
  90.  
  91.  
  92.  
  93. /* what find first/next calls look use */
  94.  
  95. typedef struct {
  96.  
  97.     char        d_buf[21];
  98.  
  99.     char        d_attribute;
  100.  
  101.     unsigned short    d_time;
  102.  
  103.     unsigned short    d_date;
  104.  
  105.     long        d_size;
  106.  
  107.     char        d_name[13];
  108.  
  109. } Dta_buf;
  110.  
  111.  
  112.  
  113. static    char    *getdirent();
  114.  
  115. static    void    setdta();
  116.  
  117. static    void    free_dircontents();
  118.  
  119.  
  120.  
  121. static    Dta_buf        dtabuf;
  122.  
  123. static    Dta_buf        *dtapnt = &dtabuf;
  124.  
  125. static    union REGS    reg, nreg;
  126.  
  127.  
  128.  
  129. #if    defined(M_I86LM)
  130.  
  131. static    struct SREGS    sreg;
  132.  
  133. #endif
  134.  
  135.  
  136.  
  137. static char *Strcpy();    /* lower-case copy */
  138.  
  139.  
  140.  
  141. DIR    *
  142.  
  143. opendir(name)
  144.  
  145.     char    *name;
  146.  
  147. {
  148.  
  149.     struct    stat        statb;
  150.  
  151.     DIR            *dirp;
  152.  
  153.     char            c;
  154.  
  155.     char            *s;
  156.  
  157.     struct _dircontents    *dp;
  158.  
  159.     char            nbuf[MAXPATHLEN + 1];
  160.  
  161.     
  162.  
  163.     /* 
  164.  
  165.      * Need special-case code for ".", to avoid problems with stat()
  166.  
  167.      * in the root directory -- WZV 891218
  168.  
  169.      */
  170.  
  171.  
  172.  
  173.     if (strcmp(name, ".") != 0 
  174.  
  175.     && (stat(name, &statb) < 0 || (statb.st_mode & S_IFMT) != S_IFDIR))
  176.  
  177.         return (DIR *) NULL;
  178.  
  179.     if (Newisnull(dirp, DIR))
  180.  
  181.         return (DIR *) NULL;
  182.  
  183.     if (*name && (c = name[strlen(name) - 1]) != '\\' && c != '/')
  184.  
  185.         (void) strcat(strcpy(nbuf, name), "\\*.*");
  186.  
  187.     else
  188.  
  189.         (void) strcat(strcpy(nbuf, name), "*.*");
  190.  
  191.     dirp->dd_loc = 0;
  192.  
  193.     setdta();
  194.  
  195.     dirp->dd_contents = dirp->dd_cp = (struct _dircontents *) NULL;
  196.  
  197.     if ((s = getdirent(nbuf)) == (char *) NULL)
  198.  
  199.         return dirp;
  200.  
  201.     do {
  202.  
  203.         if (Newisnull(dp, struct _dircontents) || (dp->_d_entry =
  204.  
  205.             malloc((unsigned) (strlen(s) + 1))) == (char *) NULL)
  206.  
  207.         {
  208.  
  209.             if (dp)
  210.  
  211.                 free((char *) dp);
  212.  
  213.             free_dircontents(dirp->dd_contents);
  214.  
  215.             return (DIR *) NULL;
  216.  
  217.         }
  218.  
  219.         if (dirp->dd_contents)
  220.  
  221.             dirp->dd_cp = dirp->dd_cp->_d_next = dp;
  222.  
  223.         else
  224.  
  225.             dirp->dd_contents = dirp->dd_cp = dp;
  226.  
  227.         (void) strcpy(dp->_d_entry, s);
  228.  
  229.         dp->_d_next = (struct _dircontents *) NULL;
  230.  
  231.     } while ((s = getdirent((char *) NULL)) != (char *) NULL);
  232.  
  233.     dirp->dd_cp = dirp->dd_contents;
  234.  
  235.  
  236.  
  237.     return dirp;
  238.  
  239. }
  240.  
  241.  
  242.  
  243. void
  244.  
  245. closedir(dirp)
  246.  
  247.     DIR    *dirp;
  248.  
  249. {
  250.  
  251.     free_dircontents(dirp->dd_contents);
  252.  
  253.     free((char *) dirp);
  254.  
  255. }
  256.  
  257.  
  258.  
  259. struct direct    *
  260.  
  261. readdir(dirp)
  262.  
  263.     DIR    *dirp;
  264.  
  265. {
  266.  
  267.     static    struct direct    dp;
  268.  
  269.     
  270.  
  271.     if (dirp->dd_cp == (struct _dircontents *) NULL)
  272.  
  273.         return (struct direct *) NULL;
  274.  
  275.     dp.d_namlen = dp.d_reclen =
  276.  
  277.         strlen(Strcpy(dp.d_name, dirp->dd_cp->_d_entry));
  278.  
  279.     dp.d_ino = 0;
  280.  
  281.     dirp->dd_cp = dirp->dd_cp->_d_next;
  282.  
  283.     dirp->dd_loc++;
  284.  
  285.  
  286.  
  287.     return &dp;
  288.  
  289. }
  290.  
  291.  
  292.  
  293. void
  294.  
  295. seekdir(dirp, off)
  296.  
  297.     DIR    *dirp;
  298.  
  299.     long    off;
  300.  
  301. {
  302.  
  303.     long            i = off;
  304.  
  305.     struct _dircontents    *dp;
  306.  
  307.  
  308.  
  309.     if (off < 0)
  310.  
  311.         return;
  312.  
  313.     for (dp = dirp->dd_contents ; --i >= 0 && dp ; dp = dp->_d_next)
  314.  
  315.         ;
  316.  
  317.     dirp->dd_loc = off - (i + 1);
  318.  
  319.     dirp->dd_cp = dp;
  320.  
  321. }
  322.  
  323.  
  324.  
  325. long
  326.  
  327. telldir(dirp)
  328.  
  329.     DIR    *dirp;
  330.  
  331. {
  332.  
  333.     return dirp->dd_loc;
  334.  
  335. }
  336.  
  337.  
  338.  
  339. static    void
  340.  
  341. free_dircontents(dp)
  342.  
  343.     struct    _dircontents    *dp;
  344.  
  345. {
  346.  
  347.     struct _dircontents    *odp;
  348.  
  349.  
  350.  
  351.     while (dp) {
  352.  
  353.         if (dp->_d_entry)
  354.  
  355.             free(dp->_d_entry);
  356.  
  357.         dp = (odp = dp)->_d_next;
  358.  
  359.         free((char *) odp);
  360.  
  361.     }
  362.  
  363. }
  364.  
  365.  
  366.  
  367. static    char    *
  368.  
  369. getdirent(dir)
  370.  
  371.     char    *dir;
  372.  
  373. {
  374.  
  375.     if (dir != (char *) NULL) {        /* get first entry */
  376.  
  377.         reg.h.ah = DOSI_FINDF;
  378.  
  379.         reg.h.cl = ATTRIBUTES;
  380.  
  381. #if    defined(M_I86LM)
  382.  
  383.         reg.x.dx = FP_OFF(dir);
  384.  
  385.         sreg.ds = FP_SEG(dir);
  386.  
  387. #else
  388.  
  389.         reg.x.dx = (unsigned) dir;
  390.  
  391. #endif
  392.  
  393.     } else {                /* get next entry */
  394.  
  395.         reg.h.ah = DOSI_FINDN;
  396.  
  397. #if    defined(M_I86LM)
  398.  
  399.         reg.x.dx = FP_OFF(dtapnt);
  400.  
  401.         sreg.ds = FP_SEG(dtapnt);
  402.  
  403. #else
  404.  
  405.         reg.x.dx = (unsigned) dtapnt;
  406.  
  407. #endif
  408.  
  409.     }
  410.  
  411. #if    defined(M_I86LM)
  412.  
  413.     intdosx(®, &nreg, &sreg);
  414.  
  415. #else
  416.  
  417.     intdos(®, &nreg);
  418.  
  419. #endif
  420.  
  421.     if (nreg.x.cflag)
  422.  
  423.         return (char *) NULL;
  424.  
  425.  
  426.  
  427.     return dtabuf.d_name;
  428.  
  429. }
  430.  
  431.  
  432.  
  433. static    void
  434.  
  435. setdta()
  436.  
  437. {
  438.  
  439.     reg.h.ah = DOSI_SDTA;
  440.  
  441. #if    defined(M_I86LM)
  442.  
  443.     reg.x.dx = FP_OFF(dtapnt);
  444.  
  445.     sreg.ds = FP_SEG(dtapnt);
  446.  
  447.     intdosx(®, &nreg, &sreg);
  448.  
  449. #else
  450.  
  451.     reg.x.dx = (int) dtapnt;
  452.  
  453.     intdos(®, &nreg);
  454.  
  455. #endif
  456.  
  457. }
  458.  
  459.  
  460.  
  461. static char *Strcpy(to,from)
  462.  
  463. register char *to,*from;
  464.  
  465. {
  466.  
  467.     register int c;
  468.  
  469.     char *start = to;
  470.  
  471.  
  472.  
  473.     while (*to++ = (islower(c = *from++) ? c : tolower(c)))
  474.  
  475.     ;
  476.  
  477.     return(start);
  478.  
  479. }
  480.  
  481. #endif
  482.  
  483.